home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / Small Eiffel 0.4.8 / lib_rand / min_stand.e < prev    next >
Text File  |  1997-04-13  |  2KB  |  100 lines

  1. -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C) 
  2. -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
  3. --
  4. class MIN_STAND
  5.    --
  6.    -- Implements the Minimal Standard generator from Press et. al.
  7.    -- Numerical Recipies.
  8.    --
  9.  
  10. inherit GEN_RAND;
  11.    
  12. creation make, with_seed
  13.  
  14. feature {NONE}
  15.  
  16.    ia: INTEGER is 16807;
  17.  
  18.    im: INTEGER is 2147483647;
  19.  
  20.    iq: INTEGER is 127773;
  21.  
  22.    ir: INTEGER is 2836;
  23.    
  24.    seed:INTEGER
  25.  
  26. feature {NONE}
  27.  
  28.    make is
  29.       local
  30.      i: INTEGER;
  31.      seed_init: INTEGER;
  32.       do
  33.      tmp_string.clear;
  34.      tmp_string.extend('0');
  35.      Current.to_pointer.append_in(tmp_string);
  36.      from
  37.         i := tmp_string.count;
  38.      until
  39.         i = 0
  40.      loop
  41.         if not tmp_string.item(i).is_digit then
  42.            tmp_string.remove(i);
  43.         end;
  44.         i := i - 1;
  45.      end;
  46.      seed_init := tmp_string.to_integer;
  47.      from
  48.      until
  49.         seed_init < im
  50.      loop
  51.         seed_init := seed_init - iq;
  52.      end;
  53.      with_seed(seed_init);
  54.       end;
  55.  
  56.    with_seed(seed_value: INTEGER) is
  57.       require
  58.      valid_seed: seed_value > 0 and seed_value < im
  59.       do
  60.      seed:= seed_value;
  61.      next;
  62.       end;
  63.    
  64. feature
  65.  
  66.    next is
  67.       local
  68.      k: INTEGER;
  69.       do
  70.      k := seed // iq;
  71.      seed := ia * (seed -k * iq) - ir * k;
  72.      if seed < 0 then
  73.         seed := seed + im;
  74.      end;
  75.       end;
  76.  
  77.    last_real: REAL is 
  78.       do
  79.      Result := (seed/im).to_real;
  80.       end;
  81.  
  82.    last_integer(n:INTEGER): INTEGER is
  83.       do
  84.      Result := seed \\ n+1;
  85.       end;
  86.  
  87. feature {NONE}
  88.  
  89.    tmp_string: STRING is 
  90.       once
  91.      !!Result.make(32);
  92.       end;
  93.  
  94. invariant
  95.  
  96.    good_seed: seed > 0 and seed < im
  97.  
  98. end -- MIN_STAND
  99.  
  100.